<?php

require_once('config.inc');

define("C_LOG_NONE",0);
define("C_LOG_ERROR",1);	
define("C_LOG_INFO",2);

/*
* class to handle site configuration
*/
class Log
{
	private static $instance;
	private static $lastCheck; /*last time we checked to see if logging is enabled*/
	private static $logLevel;
	private static $filename;
	/*
	* private constructor
	* load the configuration file
	*/
	private function __construct()
	{
		$this->logLevel = 0;
		$this->lastCheck = time() -6;
		$this->filename = str_replace("modules","site.log",dirname(__FILE__));
	}

	/*
	* should log will decide if we write to file or ignore.
	* severity gets 1 for errors, 2 for info.
	*/
	private function shouldLog($severity)
	{
		if ($this->lastCheck + 5 < time())
		{
			$config = Config::getInstance();
			$this->logLevel = $config->get("./logLevel");
			$this->lastCheck = time();
		}
		return $severity > 0 && $severity <= $this->logLevel;
	}
	
	public static function getInstance()
	{
		if(!isset(self::$instance))
		{
			$object= __CLASS__;
			self::$instance=new $object;
		}
		return self::$instance;
	}

	/* 
	*  get a config value from the config file - using xpath.
	*/
	function write($line,$severity)
	{
		if(!$this->shouldLog($severity))
			return;
			
		$fh = fopen($this->filename, 'a');
		fwrite($fh, "[".date("Y-m-d H:i:s",time())."] [$severity] $line.\n");
		fclose($fh);
	}
}

?>